<!DOCTYPE html>
<html class="no-js" lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>会员卡管理</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="//unpkg.com/vue/dist/vue.js"></script>
    <script src="//unpkg.com/element-ui@2.15.6/lib/index.js"></script>
    <style>
        @import url("//unpkg.com/element-ui@2.15.6/lib/theme-chalk/index.css");
        #main {
            position: absolute;
            width: 65%;
            height: 1px;
            top: 50px;
            left: 50%;
            transform: translate(-50%, 0);
        }
        #add {
            float: right;
        }
    </style>
</head>
<body>
<div id="app">
    <el-page-header @back="goBack" content="会员卡管理页面">
    </el-page-header>
    <div id="main">
        <template>
            <div id="add">
                <el-button type="info" @click="addCard(0)" plain>生成白银卡</el-button>
                <el-button type="warning" @click="addCard(1)" plain>生成黄金卡</el-button>
                <el-button type="primary" @click="addCard(2)" plain>生成钻石卡</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%">
                <el-table-column
                        align="center"
                        prop="id"
                        label=""
                        width="150">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="grade"
                        label="会员卡等级"
                        width="180">
                    <template slot-scope="scope">
                        <el-tag type="info" v-if="scope.row.grade == 0">
                            白银卡
                        </el-tag>
                        <el-tag type="warning" v-if="scope.row.grade == 1">
                            黄金卡
                        </el-tag>
                        <el-tag v-if="scope.row.grade == 2">
                            钻石卡
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="cardId"
                        label="会员卡ID">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="id"
                        label="操作"
                        width="210">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                @click="idTemp = scope.row.id; drawer = true">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </template>
        <br/><br/><br/>


    </div>
    <!--handleEdit(scope.$index, scope.row)-->
    <!--编辑弹出框-->
    <el-drawer
            title="修改会员卡等级等级"
            :visible.sync="drawer"
            align="center">
        <el-button type="info" @click="toUpdateCard(0,idTemp)" plain>白银卡</el-button>
        <el-button type="warning" @click="toUpdateCard(1,idTemp)" plain>黄金卡</el-button>
        <el-button type="primary" @click="toUpdateCard(2,idTemp)"  plain>钻石卡</el-button>
    </el-drawer>
</div>
<script th:inline="javascript">
    var Main = {
        data() {
            return {
                tableData: JSON.parse([[${cardsJson}]]),
                drawer: false,
                idTemp: 0
            }
        },
        methods: {

            goBack() {
                history.back();
            },
            addCard: function (flag) {
                axios.get('/admin/add/card', {
                    params: {
                        flag: flag
                    }
                }).then((response) => {
                    if (response.data === 1) {
                        this.$alert('生成会员卡成功~！', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                window.location.assign("/admin/card");
                            }
                        });
                    } else {
                        this.$message({
                            message: '出现问题，请重试~！',
                            type: 'error'
                        });
                    }
                });
            },

            toUpdateCard: function(flag,id){
                axios.get('/admin/update/card', {
                    params: {
                        flag: flag,
                        id: id
                    }
                }).then((response) => {
                    if (response.data === 1) {
                        this.$alert('会员卡修改成功~！', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                window.location.assign("/admin/card");
                            }
                        });
                    } else {
                        this.$message({
                            message: '出现问题，请重试~！',
                            type: 'error'
                        });
                    }
                });


            },

            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该会员卡, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get('/admin/del/card', {
                        params: {
                            id: row.id,
                        }
                    }).then((response) => {
                        switch (response.data) {
                            case 0:
                                this.$message({
                                    message: '删除失败~！',
                                    type: 'error'
                                });
                                break;
                            case 1:
                                this.$alert('删除成功~！', '提示', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        window.location.assign("/admin/card");
                                    }
                                });
                                break;
                            default:
                                this.$message({
                                    message: '出错了，请重试~！',
                                    type: 'warning'
                                });
                                break;
                        }
                    }).catch((error) => {
                        this.$message({
                            message: '出错了，请重试~！',
                            type: 'warning'
                        });
                    });
                });
            }
        }
    };
    var Ctor = Vue.extend(Main);
    new Ctor().$mount('#app')
</script>
</body>
</html>